<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="公众号" prop="accountId">
        <WxAccountSelect @change="onAccountChanged" />
      </el-form-item>
      <el-form-item label="模板名称" prop="templateId">
        <el-input
          v-model="queryParams.templateId"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="标题" prop="nickname">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"> <Icon icon="ep:search" />搜索 </el-button>
        <el-button @click="resetQuery"> <Icon icon="ep:refresh" />重置 </el-button>
        <el-button
          type="success"
          plain
          @click="handleSync"
          v-hasPermi="['mp:template:sync']"
          :disabled="queryParams.accountId === 0"
        >
          <Icon icon="ep:refresh" class="mr-5px" /> 同步公众号模板
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="编号" align="center" prop="id" width="130" />
      <el-table-column label="模版名称" align="center" prop="templateId" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="模版内容" align="center" prop="content" width="350" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            type="primary"
            link
            @click="sendMessage(scope.row.id, queryParams.accountId)"
            v-hasPermi="['mp:template:update']"
          >
            推送消息
          </el-button>
          <el-button
            type="primary"
            link
            @click="openForm(scope.row.id)"
            v-hasPermi="['mp:template:update']"
          >
            配置
          </el-button>
          <el-button
            type="danger"
            link
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['mp:template:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：修改 -->
  <TemplateForm ref="formRef" @success="getList" />
  <SendMessage ref="sendRef" @success="getList" />
</template>
<script lang="ts" setup>
import * as MpTemplateApi from '@/api/mp/template'
import WxAccountSelect from '@/views/mp/components/wx-account-select'
import type { FormInstance } from 'element-plus'
import TemplateForm from './TemplateForm.vue'
import SendMessage from './SendMessage.vue'

defineOptions({ name: 'MpUser' })

const message = useMessage() // 消息

const { t } = useI18n()

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref<any[]>([]) // 列表的数据

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  accountId: -1,
  templateId: '',
  title: ''
})
const queryFormRef = ref<FormInstance | null>(null) // 搜索的表单

/** 侦听公众号变化 **/
const onAccountChanged = (id: number) => {
  queryParams.accountId = id
  queryParams.pageNo = 1
  getList()
}

/** 查询列表 */
const getList = async () => {
  try {
    loading.value = true
    const data = await MpTemplateApi.getTemplatePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  const accountId = queryParams.accountId
  queryFormRef.value?.resetFields()
  queryParams.accountId = accountId
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref(null)
const openForm = (id: number) => {
  formRef.value?.open(id)
}

/** 发送消息操作 */
const sendRef = ref(null)
const sendMessage = (id: number, accountId: number) => {
  sendRef.value?.open(id, accountId)
}

/** 删除 */
const handleDelete = async (id) => {
  // 删除的二次确认
  await message.delConfirm()
  // 发起删除
  await MpTemplateApi.deleteTemplate(id)
  message.success(t('common.delSuccess'))
  // 刷新列表
  await getList()
}
/** 同步标签 */
const handleSync = async () => {
  try {
    await message.confirm('是否确认同步公众号模板？')
    await MpTemplateApi.syncTemplate(queryParams.accountId)
    message.success('开始从微信公众号同步模板信息，同步需要一段时间，建议稍后再查询')
    await getList()
  } catch {}
}
</script>
